package com.example.springbootmybatis.controller;

import com.example.springbootmybatis.entity.*;
import com.example.springbootmybatis.mapper.UserMapper;
import com.example.springbootmybatis.util.ToolUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    //从spring容器中得到对象
    @Autowired
    UserMapper userMapper;

    //对外暴漏的接口地址(http://localhost:9000/user/getAllUsers)
    @RequestMapping("/getAllUsers")
    public ResultMap<? extends Object> getAllUsers(@RequestBody UserEntity entity) {
        PageHelper.startPage(entity.getPageindex(), entity.getPagesize());
        List<UserEntity> users = userMapper.getAllUsers();
        PageInfo<?> resultForPage = ToolUtil.getResultForPage(users);
        return ToolUtil.getResult(resultForPage, "查询");
    }


    @RequestMapping("/getUsersByName/{username}")
    public ResultMap<? extends Object> getAllUsers(@PathVariable("username") String name
    ) {
        List<UserEntity> users = userMapper.getUsersByName(name);
        return ToolUtil.getResult(users, "查询");
    }



    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/login")
    //网络传输时不支持对象 支持字符串  会把对象转换成字符串 到了后台再把字符串转换成对象 （@RequestBody）
    public ResultMap<? extends Object> login(@RequestBody UserEntity userEntity) throws ParseException {

        UserEntity loginUser = userMapper.login(userEntity);
        if (loginUser != null) {
            ToolUtil.setRedisDataVisit(redisTemplate);
            log.info("访问量  "+redisTemplate.opsForValue().get(ToolUtil.DateFormat(new Date()) + "_visitor"));
        }
        return ToolUtil.getResult(loginUser, "登录");
    }

    @RequestMapping("/updateUserById")
    public ResultMap<String> updateUserById(UserEntity userEntity) {
        int i = userMapper.updateUserByID(userEntity);
        ResultMap<String> resultMap = new ResultMap<>();
        if (i == 1) {
            resultMap.setCode(200);
            resultMap.setMsg("修改成功");
        } else {
            resultMap.setCode(-1);
            resultMap.setMsg("修改失败");
        }
        return resultMap;

    }

    @RequestMapping("/registUser")
    public ResultMap<? extends Object> registeUser(@RequestBody UserEntity entity) {
        int i = userMapper.insertUser(entity);
        return ToolUtil.getResult(i, "注册");
    }

    @RequestMapping("/deleteUserByIds")
    public ResultMap<? extends Object> deleteUserByIds(@RequestBody List<Integer> ids) {
        int i = userMapper.deleteUserByIds(ids);
        return ToolUtil.getResult(i, "删除");
    }


    @RequestMapping("/checkIsExist/{username}")
    public ResultMap<? extends Object> checkIsExist(@PathVariable("username") String username) {
        int i = userMapper.checkIsExist(username);
        String msg = "";
        if (i > 0) {
            msg = "该用户名已经存在";

        } else {
            msg = "该用户名可用";
        }
        return ToolUtil.getResult(i, msg);
    }


    @RequestMapping("/deleteUserById")
    public ResultMap<String> deleteUserById(Integer uid) {
        int i = userMapper.deleteUserByid(uid);
        ResultMap<String> resultMap = new ResultMap<>();
        if (i == 1) {
            resultMap.setCode(200);
            resultMap.setMsg("删除成功");
//            getAllUsers();
        } else {
            resultMap.setCode(-1);
            resultMap.setMsg("删除失败");

        }
        return resultMap;

    }


}
